package com.xs.mvc.service;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xs.mvc.dao.BaseDao;
import com.xs.mvc.query.BaseQuery;

/**
 * @Title:
 *         <P>
 *         BaseServiceImpl.java
 *         </P>
 * @Package
 *          <P>
 *          com.xs.mvc.service
 *          </P>
 * @Description:
 *               <P>
 *               BaseServiceImpl
 *               </P>
 * @author renweiping
 * @date 2019年1月15日 下午3:57:01
 * @version 1.0
 */
@Transactional
public abstract class BaseServiceImpl<T, PK extends Serializable> implements BaseService<T, PK> {
	public static Logger LOG = Logger.getLogger(BaseServiceImpl.class);

	protected abstract BaseDao<T, PK> getBaseDao();

	@Transactional(readOnly = true)
	public T getById(PK id) {
		return getBaseDao().getById(id);
	}

	public int deleteById(PK id) {
		return getBaseDao().deleteById(id);
	}

	public void save(T t) {
		getBaseDao().save(t);
	}

	@Transactional(readOnly = true)
	public List<T> getAll() {
		return getBaseDao().getAll();
	}

	public int update(T t) {
		return getBaseDao().update(t);
	}

	public PageInfo<T> getByPage(BaseQuery query) {

		int pageNum = query.getPageNum();
		if (pageNum <= 0) {
			throw new IllegalAccessError("分页【页码】设置错误，参数应该大于0，原始参数为：" + pageNum);
		}

		int pageSize = query.getPageSize();
		if (pageSize <= 0) {
			throw new IllegalAccessError("分页【页大小】设置错误，参数应该大于0，原始参数为：" + pageSize);
		}

		PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize, true);
		List<T> result = this.getBaseDao().getByPage(query);
		PageInfo<T> pageInfo = new PageInfo<T>(result);
		return pageInfo;
	}

	/**
	 * @Description: 删除all
	 * @return int  
	 * @throws
	 * @author renweiping
	  *@e_mail renweiping_1113@google.com
	 * @date 2019年2月15日
	 */
	@Override
	public int deleteAll() {
		// TODO Auto-generated method stub
		return getBaseDao().deleteAll();
	}

}
